package io.renren.modules.task.controller;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import io.renren.common.utils.HttpClientUtil;
import io.renren.common.utils.R;
import io.renren.modules.sys.controller.AbstractController;
import io.renren.modules.task.constant.ManagePublicMethod;
import io.renren.modules.task.entity.MtCcAgvCommunLogEntity;
import io.renren.modules.task.entity.MtCcEquipmentRunInfoEntity;
import io.renren.modules.task.entity.MtCcEquipmentRunInfoLogEntity;
import io.renren.modules.task.enums.MtCcAGVEnum;
import io.renren.modules.task.enums.MtCcEnum;
import io.renren.modules.task.model.AgvParam;
import io.renren.modules.task.model.MtCcAreaRouteEntityVo;
import io.renren.modules.task.model.Position;
import io.renren.modules.task.service.MtCcAgvCommunLogService;
import io.renren.modules.task.service.MtCcAreaRouteService;
import io.renren.modules.task.service.MtCcEquipmentRunInfoLogService;
import io.renren.modules.task.service.MtCcEquipmentRunInfoService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.Date;
import java.util.List;
import java.util.Map;

import static io.renren.modules.task.constant.ManageConstant.CALL_MATERIAL_ORDER_TYPE;
import static io.renren.modules.task.constant.ManageConstant.CALL_MOUTH_ORDER_TYPE;

/**
 * @Author: weihongyang
 * @Date: 2019/8/14 9:16
 * @Version 1.0
 */

@Api(description = "呼叫转嘴API")
@RestController
@RequestMapping("/call/mouth")
public class CallMouthController extends AbstractController {

    @Autowired
    private MtCcAreaRouteService mtCcAreaRouteService;

    @Autowired
    private MtCcEquipmentRunInfoLogService mtCcEquipmentRunInfoLogService;

    @Autowired
    private MtCcEquipmentRunInfoService mtCcEquipmentRunInfoService;

    @Autowired
    private MtCcAgvCommunLogService mtCcAgvCommunLogService;


    @ApiOperation(value = "操作台叫转嘴操作" , notes = "操作台叫转嘴操作")
    @RequestMapping("/insert/data")
    public R insertData(){
        Date date = new Date();
        Integer count = mtCcEquipmentRunInfoLogService.selectCountByTaskType(3)+1;
        String reqCode = ManagePublicMethod.creatOrderCode(CALL_MOUTH_ORDER_TYPE,
                String.valueOf(getUser().getConsoleCode()),String.valueOf(count));
        MtCcEquipmentRunInfoEntity mtCcEquipmentRunInfoEntity = new MtCcEquipmentRunInfoEntity();
        mtCcEquipmentRunInfoEntity.setCreateTime(date);
        mtCcEquipmentRunInfoEntity.setEquipmentId(getUser().getConsoleCode());
        mtCcEquipmentRunInfoEntity.setUserId(getUser().getUserId().toString());
        mtCcEquipmentRunInfoEntity.setTaskName(MtCcEnum.CALLMOUTH.getMsg());
        mtCcEquipmentRunInfoEntity.setTaskType(MtCcEnum.CALLMOUTH.getCode());
        mtCcEquipmentRunInfoEntity.setStatus(0);
        mtCcEquipmentRunInfoEntity.setTaskCode(reqCode);
        mtCcEquipmentRunInfoEntity.setEnable(0);
        mtCcEquipmentRunInfoService.save(mtCcEquipmentRunInfoEntity);

        MtCcEquipmentRunInfoLogEntity mtCcEquipmentRunInfoLogEntity = new MtCcEquipmentRunInfoLogEntity();
        mtCcEquipmentRunInfoLogEntity.setCreateTime(date);
        mtCcEquipmentRunInfoLogEntity.setEquipmentId(getUser().getConsoleCode());
        mtCcEquipmentRunInfoLogEntity.setUserId(getUserId().toString());
        mtCcEquipmentRunInfoLogEntity.setTaskName(MtCcEnum.CALLMOUTH.getMsg());
        mtCcEquipmentRunInfoLogEntity.setTaskType(MtCcEnum.CALLMOUTH.getCode());
        mtCcEquipmentRunInfoLogEntity.setTaskCode(reqCode);
        mtCcEquipmentRunInfoLogEntity.setEnable(0);
        mtCcEquipmentRunInfoLogEntity.setStatus(1);
        mtCcEquipmentRunInfoLogService.save(mtCcEquipmentRunInfoLogEntity);

        return R.ok();
    }

    @ApiOperation(value = "转嘴放料台操作" , notes = "转嘴放料台操作")
    @PostMapping("/select/rounte/to/agv")
    public R selectRounteToAgv(@RequestBody Map<String, Object> params) throws Exception{
        String url = MtCcAGVEnum.AGVURL_callMaterial.getCode();
        List<MtCcAreaRouteEntityVo> list = mtCcAreaRouteService.selectRouteCodeByTaskType(params);
        String reqCode = list.get(0).getTaskCode();
        String taskTyp = "HJZU";
        Position position = new Position();
        position.setPositionCode(list.get(0).getSourceNo());
        position.setType("00");
        Position position1 = new Position();
        position1.setPositionCode(list.get(0).getDestinationNo());
        position1.setType("00");
        Object[] positionCodePath = {position,position1};
        AgvParam agvParam = new AgvParam();
        agvParam.setReqCode(reqCode);
        agvParam.setTaskCode(reqCode);
        agvParam.setAgvCode("3002");
        agvParam.setTaskTyp(taskTyp);
        agvParam.setPositionCodePath(positionCodePath);
        String resultMsg = null;//获取服务端返回的参数
        String param = JSONObject.toJSONString(agvParam);//将对象转换为JSON格式


        MtCcAgvCommunLogEntity mtCcAgvCommunLogEntity = new MtCcAgvCommunLogEntity();
        Date date = new Date();
        mtCcAgvCommunLogEntity.setCreateTime(date);
        mtCcAgvCommunLogEntity.setEquipmentId(getUser().getConsoleCode());
        mtCcAgvCommunLogEntity.setStatus(0);
        mtCcAgvCommunLogEntity.setContent("agv日志已生成");
        mtCcAgvCommunLogEntity.setTaskCode(reqCode);
        mtCcAgvCommunLogService.save(mtCcAgvCommunLogEntity);


        resultMsg = HttpClientUtil.post(url, param);// 以post形式调用webservice
        JSONObject jsonObject = JSON.parseObject(resultMsg);
        String code = jsonObject.getString("code");
        if(code.equals("0")){
            MtCcEquipmentRunInfoEntity mtCcEquipmentRunInfoEntity = new MtCcEquipmentRunInfoEntity();
            mtCcEquipmentRunInfoEntity.setStatus(1);
            mtCcEquipmentRunInfoService.update(mtCcEquipmentRunInfoEntity,
                    new QueryWrapper<MtCcEquipmentRunInfoEntity>().eq("task_code",params.get("taskCode")));
            mtCcAgvCommunLogEntity.setStatus(1);
            mtCcAgvCommunLogEntity.setContent("agv日志已生成,调用agv接口成功");
            mtCcAgvCommunLogService.update(mtCcAgvCommunLogEntity,new QueryWrapper<MtCcAgvCommunLogEntity>().eq(
                    "task_code",reqCode));
            return R.ok("成功");
        }else if(code.equals("1")){
            return R.error("参数相关的错误");
        }else{
            return R.error("其他未知错误");
        }
    }


    @ApiOperation(value = "转嘴操作台确认" , notes = "转嘴操作台确认")
    @PostMapping("/call/agv/success")
    public R callAgvSuccess(@RequestBody Map<String, Object> params) throws Exception{
        String url = MtCcAGVEnum.AGVURL_callMaterialTouchOff.getCode();
        List<MtCcAreaRouteEntityVo> list = mtCcAreaRouteService.selectMouthCarPointByTaskType(params);
        String reqCode = list.get(0).getTaskCode()+2;
        String wbCode = list.get(0).getDestinationNo();
        AgvParam agvParam = new AgvParam();
        agvParam.setReqCode(reqCode);
        agvParam.setWbCode(wbCode);
        String resultMsg = null;//获取服务端返回的参数
        String param = JSONObject.toJSONString(agvParam);//将对象转换为JSON格式

        MtCcAgvCommunLogEntity mtCcAgvCommunLogEntity = new MtCcAgvCommunLogEntity();
        Date date = new Date();
        mtCcAgvCommunLogEntity.setCreateTime(date);
        mtCcAgvCommunLogEntity.setEquipmentId(getUser().getConsoleCode());
        mtCcAgvCommunLogEntity.setStatus(0);
        mtCcAgvCommunLogEntity.setContent("agv日志已生成");
        mtCcAgvCommunLogEntity.setTaskCode(reqCode);
        mtCcAgvCommunLogService.save(mtCcAgvCommunLogEntity);


        resultMsg = HttpClientUtil.post(url, param);// 以post形式调用webservice
        JSONObject jsonObject = JSON.parseObject(resultMsg);
        String code = jsonObject.getString("code");
        if(code.equals("0")){
            MtCcEquipmentRunInfoEntity mtCcEquipmentRunInfoEntity = new MtCcEquipmentRunInfoEntity();
            mtCcEquipmentRunInfoEntity.setStatus(4);
            mtCcEquipmentRunInfoService.update(mtCcEquipmentRunInfoEntity,
                    new QueryWrapper<MtCcEquipmentRunInfoEntity>().eq("task_code",params.get("taskCode")));
            mtCcAgvCommunLogEntity.setStatus(1);
            mtCcAgvCommunLogEntity.setContent("agv日志已生成,调用agv接口成功");
            mtCcAgvCommunLogService.update(mtCcAgvCommunLogEntity,new QueryWrapper<MtCcAgvCommunLogEntity>().eq(
                    "task_code",reqCode));
            return R.ok("成功");
        }else if(code.equals("1")){
            return R.error("参数相关的错误");
        }else{
            return R.error("其他未知错误");
        }
    }

}

